﻿using System;
using System.IO;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000068 RID: 104
    public static class ShouQianBaProxy {
        // Token: 0x0600036A RID: 874 RVA: 0x0000426A File Offset: 0x0000246A
        public static void AddShouQianBaProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/upay/v2/pay", new HttpContextActionDelegate(Process));
            svr.RouterList.Add("POST/upay/v2/query", new HttpContextActionDelegate(Process));
        }

        // Token: 0x0600036B RID: 875 RVA: 0x0000EC68 File Offset: 0x0000CE68
        private static void Process(HttpContext ctx) {
            string json = new StreamReader(ctx.Request).ReadToEnd();
            ShouQianBaRequest shouQianBaRequest = json.JsonDecode<ShouQianBaRequest>();
            ShouQianBaResult shouQianBaResult = new ShouQianBaResult();
            shouQianBaResult.biz_response.data.client_sn = shouQianBaRequest.client_sn;
            try {
                HongshPayTrade hongshPayTrade;
                if (ctx.Request.Url.AbsolutePath.Equals("/upay/v2/pay", StringComparison.OrdinalIgnoreCase)) {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Amount = shouQianBaRequest.total_amount,
                        ClientTradeId = shouQianBaRequest.client_sn
                    });
                }
                else {
                    if (!ctx.Request.Url.AbsolutePath.Equals("/upay/v2/query", StringComparison.OrdinalIgnoreCase)) {
                        throw new Exception("不支持的接口方法！");
                    }
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        ClientTradeId = shouQianBaRequest.client_sn
                    });
                }
                if (hongshPayTrade.State == TradeState.未知) {
                    shouQianBaResult.biz_response.result_code = "SUCCESS";
                    shouQianBaResult.biz_response.error_code_standard = "EP104";
                    shouQianBaResult.biz_response.data.status = "IN_PROG";
                    shouQianBaResult.biz_response.data.order_status = "CREATED";
                }
                else if (hongshPayTrade.State == TradeState.成功) {
                    shouQianBaResult.biz_response.data.finish_time = hongshPayTrade.UpdateTime.ToString();
                    shouQianBaResult.biz_response.result_code = "PAY_SUCCESS";
                    shouQianBaResult.biz_response.data.status = "SUCCESS";
                    shouQianBaResult.biz_response.data.order_status = "PAID";
                }
                else {
                    shouQianBaResult.biz_response.error_message = hongshPayTrade.StateDescript;
                    shouQianBaResult.biz_response.error_code_standard = "EP99";
                    shouQianBaResult.biz_response.data.status = "PAY_CANCELED";
                    shouQianBaResult.biz_response.data.order_status = "FAIL_CANCELED";
                }
                shouQianBaResult.biz_response.data.sn = hongshPayTrade.TradeId;
                shouQianBaResult.biz_response.data.payer_uid = hongshPayTrade.Payer;
                shouQianBaResult.biz_response.data.payway_name = hongshPayTrade.Payway.ToString();
                shouQianBaResult.biz_response.data.total_amount = hongshPayTrade.Amount.ToString();
                shouQianBaResult.biz_response.data.settlement_amount = hongshPayTrade.ActualAmount.ToString();
                switch (hongshPayTrade.Payway) {
                    case TradePayway.微信:
                        shouQianBaResult.biz_response.data.payway = "3";
                        break;
                    case TradePayway.支付宝:
                        shouQianBaResult.biz_response.data.payway = "1";
                        break;
                    case TradePayway.云闪付:
                        shouQianBaResult.biz_response.data.payway = "4";
                        break;
                    default:
                        shouQianBaResult.biz_response.data.payway = "0";
                        break;
                }
            }
            catch (Exception ex) {
                shouQianBaResult.biz_response.result_code = "PAY_FAIL";
                shouQianBaResult.biz_response.error_code_standard = "EP99";
                shouQianBaResult.biz_response.error_code = "EXTERNAL_SERVICE_EXCEPTION";
                shouQianBaResult.biz_response.error_message = ex.Message;
            }
            ctx.Result = new HttpJsonResult(shouQianBaResult);
        }

        // Token: 0x02000069 RID: 105
        public class ShouQianBaRequest {
            // Token: 0x17000179 RID: 377
            // (get) Token: 0x0600036C RID: 876 RVA: 0x000042A4 File Offset: 0x000024A4
            // (set) Token: 0x0600036D RID: 877 RVA: 0x000042AC File Offset: 0x000024AC
            public string terminal_sn { get; set; }

            // Token: 0x1700017A RID: 378
            // (get) Token: 0x0600036E RID: 878 RVA: 0x000042B5 File Offset: 0x000024B5
            // (set) Token: 0x0600036F RID: 879 RVA: 0x000042BD File Offset: 0x000024BD
            public string client_sn { get; set; }

            // Token: 0x1700017B RID: 379
            // (get) Token: 0x06000370 RID: 880 RVA: 0x000042C6 File Offset: 0x000024C6
            // (set) Token: 0x06000371 RID: 881 RVA: 0x000042CE File Offset: 0x000024CE
            public string device_id { get; set; }

            // Token: 0x1700017C RID: 380
            // (get) Token: 0x06000372 RID: 882 RVA: 0x000042D7 File Offset: 0x000024D7
            // (set) Token: 0x06000373 RID: 883 RVA: 0x000042DF File Offset: 0x000024DF
            public string dynamic_id { get; set; }

            // Token: 0x1700017D RID: 381
            // (get) Token: 0x06000374 RID: 884 RVA: 0x000042E8 File Offset: 0x000024E8
            // (set) Token: 0x06000375 RID: 885 RVA: 0x000042F0 File Offset: 0x000024F0
            public int total_amount { get; set; }

            // Token: 0x06000376 RID: 886 RVA: 0x0000286D File Offset: 0x00000A6D
            public ShouQianBaRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x0200006A RID: 106
        public class ShouQianBaResult {
            // Token: 0x1700017E RID: 382
            // (get) Token: 0x06000377 RID: 887 RVA: 0x000042F9 File Offset: 0x000024F9
            // (set) Token: 0x06000378 RID: 888 RVA: 0x00004301 File Offset: 0x00002501
            public string result_code { get; set; }

            // Token: 0x1700017F RID: 383
            // (get) Token: 0x06000379 RID: 889 RVA: 0x0000430A File Offset: 0x0000250A
            // (set) Token: 0x0600037A RID: 890 RVA: 0x00004312 File Offset: 0x00002512
            public string error_code { get; set; }

            // Token: 0x17000180 RID: 384
            // (get) Token: 0x0600037B RID: 891 RVA: 0x0000431B File Offset: 0x0000251B
            // (set) Token: 0x0600037C RID: 892 RVA: 0x00004323 File Offset: 0x00002523
            public string error_message { get; set; }

            // Token: 0x17000181 RID: 385
            // (get) Token: 0x0600037D RID: 893 RVA: 0x0000432C File Offset: 0x0000252C
            // (set) Token: 0x0600037E RID: 894 RVA: 0x00004334 File Offset: 0x00002534
            public ShouQianBaResultBiz biz_response { get; set; }

            // Token: 0x0600037F RID: 895 RVA: 0x0000433D File Offset: 0x0000253D
            public ShouQianBaResult() {
                //Class15.cwcZ3SWztHroi();
                result_code = "200";
                //base..ctor();
            }

            // Token: 0x0200006B RID: 107
            public class ShouQianBaResultBiz {
                // Token: 0x17000182 RID: 386
                // (get) Token: 0x06000380 RID: 896 RVA: 0x00004355 File Offset: 0x00002555
                // (set) Token: 0x06000381 RID: 897 RVA: 0x0000435D File Offset: 0x0000255D
                public string result_code { get; set; }

                // Token: 0x17000183 RID: 387
                // (get) Token: 0x06000382 RID: 898 RVA: 0x00004366 File Offset: 0x00002566
                // (set) Token: 0x06000383 RID: 899 RVA: 0x0000436E File Offset: 0x0000256E
                public string error_code { get; set; }

                // Token: 0x17000184 RID: 388
                // (get) Token: 0x06000384 RID: 900 RVA: 0x00004377 File Offset: 0x00002577
                // (set) Token: 0x06000385 RID: 901 RVA: 0x0000437F File Offset: 0x0000257F
                public string error_code_standard { get; set; }

                // Token: 0x17000185 RID: 389
                // (get) Token: 0x06000386 RID: 902 RVA: 0x00004388 File Offset: 0x00002588
                // (set) Token: 0x06000387 RID: 903 RVA: 0x00004390 File Offset: 0x00002590
                public string error_message { get; set; }

                // Token: 0x17000186 RID: 390
                // (get) Token: 0x06000388 RID: 904 RVA: 0x00004399 File Offset: 0x00002599
                // (set) Token: 0x06000389 RID: 905 RVA: 0x000043A1 File Offset: 0x000025A1
                public ShouQianBaResultBizData data { get; set; }

                // Token: 0x0600038A RID: 906 RVA: 0x0000286D File Offset: 0x00000A6D
                public ShouQianBaResultBiz() {
                    //Class15.cwcZ3SWztHroi();
                    //base..ctor();
                }

                // Token: 0x0200006C RID: 108
                public class ShouQianBaResultBizData {
                    // Token: 0x17000187 RID: 391
                    // (get) Token: 0x0600038B RID: 907 RVA: 0x000043AA File Offset: 0x000025AA
                    // (set) Token: 0x0600038C RID: 908 RVA: 0x000043B2 File Offset: 0x000025B2
                    public string sn { get; set; }

                    // Token: 0x17000188 RID: 392
                    // (get) Token: 0x0600038D RID: 909 RVA: 0x000043BB File Offset: 0x000025BB
                    // (set) Token: 0x0600038E RID: 910 RVA: 0x000043C3 File Offset: 0x000025C3
                    public string client_sn { get; set; }

                    // Token: 0x17000189 RID: 393
                    // (get) Token: 0x0600038F RID: 911 RVA: 0x000043CC File Offset: 0x000025CC
                    public string client_tsn {
                        get {
                            return client_sn;
                        }
                    }

                    // Token: 0x1700018A RID: 394
                    // (get) Token: 0x06000390 RID: 912 RVA: 0x000043CC File Offset: 0x000025CC
                    public string reflect {
                        get {
                            return client_sn;
                        }
                    }

                    // Token: 0x1700018B RID: 395
                    // (get) Token: 0x06000391 RID: 913 RVA: 0x000043D4 File Offset: 0x000025D4
                    public string trade_no {
                        get {
                            return sn;
                        }
                    }

                    // Token: 0x1700018C RID: 396
                    // (get) Token: 0x06000392 RID: 914 RVA: 0x000043DC File Offset: 0x000025DC
                    // (set) Token: 0x06000393 RID: 915 RVA: 0x000043E4 File Offset: 0x000025E4
                    public string finish_time { get; set; }

                    // Token: 0x1700018D RID: 397
                    // (get) Token: 0x06000394 RID: 916 RVA: 0x000043ED File Offset: 0x000025ED
                    public string channel_finish_time {
                        get {
                            return finish_time;
                        }
                    }

                    // Token: 0x1700018E RID: 398
                    // (get) Token: 0x06000395 RID: 917 RVA: 0x000043F5 File Offset: 0x000025F5
                    // (set) Token: 0x06000396 RID: 918 RVA: 0x000043FD File Offset: 0x000025FD
                    public string status { get; set; }

                    // Token: 0x1700018F RID: 399
                    // (get) Token: 0x06000397 RID: 919 RVA: 0x00004406 File Offset: 0x00002606
                    // (set) Token: 0x06000398 RID: 920 RVA: 0x0000440E File Offset: 0x0000260E
                    public string order_status { get; set; }

                    // Token: 0x17000190 RID: 400
                    // (get) Token: 0x06000399 RID: 921 RVA: 0x00004417 File Offset: 0x00002617
                    // (set) Token: 0x0600039A RID: 922 RVA: 0x0000441F File Offset: 0x0000261F
                    public string payway { get; set; }

                    // Token: 0x17000191 RID: 401
                    // (get) Token: 0x0600039B RID: 923 RVA: 0x00004428 File Offset: 0x00002628
                    // (set) Token: 0x0600039C RID: 924 RVA: 0x00004430 File Offset: 0x00002630
                    public string payway_name { get; set; }

                    // Token: 0x17000192 RID: 402
                    // (get) Token: 0x0600039D RID: 925 RVA: 0x00004439 File Offset: 0x00002639
                    public string sub_payway { get; }

                    // Token: 0x17000193 RID: 403
                    // (get) Token: 0x0600039E RID: 926 RVA: 0x00004441 File Offset: 0x00002641
                    // (set) Token: 0x0600039F RID: 927 RVA: 0x00004449 File Offset: 0x00002649
                    public string payer_uid { get; set; }

                    // Token: 0x17000194 RID: 404
                    // (get) Token: 0x060003A0 RID: 928 RVA: 0x00004452 File Offset: 0x00002652
                    // (set) Token: 0x060003A1 RID: 929 RVA: 0x0000445A File Offset: 0x0000265A
                    public string total_amount { get; set; }

                    // Token: 0x17000195 RID: 405
                    // (get) Token: 0x060003A2 RID: 930 RVA: 0x00004463 File Offset: 0x00002663
                    public string net_amount {
                        get {
                            return total_amount;
                        }
                    }

                    // Token: 0x17000196 RID: 406
                    // (get) Token: 0x060003A3 RID: 931 RVA: 0x0000446B File Offset: 0x0000266B
                    // (set) Token: 0x060003A4 RID: 932 RVA: 0x00004473 File Offset: 0x00002673
                    public string settlement_amount { get; set; }

                    // Token: 0x060003A5 RID: 933 RVA: 0x0000447C File Offset: 0x0000267C
                    public ShouQianBaResultBizData() {
                        // Class15.cwcZ3SWztHroi();
                        sub_payway = "1";
                        // base..ctor();
                    }
                }
            }
        }
    }
}
